package com.ss.android.article.browser.search.b;

import android.app.Application;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import inline.FileUtils;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;

public abstract class b {
    private static final long a = TimeUnit.DAYS.toSeconds(1);
    @NonNull
    private static final Interceptor e = new c();

    @NonNull
    protected final OkHttpClient client;
    @NonNull
    private final CacheControl c;
    @NonNull
    private final String d;

    b(@NonNull Application arg7, @NonNull String arg8) {
        this.d = arg8;
        TextUtils.isEmpty(Locale.getDefault().getLanguage());
        File v0 = new File(arg7.getCacheDir(), "suggestion_responses");
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.cache(new Cache(v0, com.ss.android.article.browser.c.d.a()));
        builder.internalCache = null;
        this.client = builder.addNetworkInterceptor(e).build();
        this.c = new CacheControl.Builder().maxAge(1, TimeUnit.DAYS).build();
    }

    static long a() {
        return a;
    }

    @NonNull
    protected abstract String a(@NonNull String arg1);

    protected abstract void a(@NonNull InputStream arg1, @NonNull List arg2) throws Exception;

    @NonNull
    public final List b(@NonNull String arg6) {
        String v1_1;
        ArrayList v0 = new ArrayList(5);
        try {
            v1_1 = URLEncoder.encode(arg6, this.d);
        } catch (UnsupportedEncodingException v1) {
            Log.e("BaseSuggestionsModel", "Unable to encode the URL", v1);
            return v0;
        }

        InputStream v1_2 = this.c(v1_1);
        if (v1_2 != null) {
            try {
                this.a(v1_2, v0);
            } catch (Exception v2) {
                Log.e("BaseSuggestionsModel", "Unable to parse results", ((Throwable) v2));
                return v0;
            } finally {
                FileUtils.a(((Closeable) v1_2));
            }
        }
        return ((List) v0);
    }

    @Nullable
    private InputStream c(@NonNull String arg6) {
        InputStream v0 = null;
        String v1 = this.a(arg6);
        try {
            URL v2 = new URL(v1);
            Request.Builder v1_2 = new Request.Builder();
            HttpUrl v3 = HttpUrl.get(v2);
            if (v3 == null) {
                throw new IllegalArgumentException("unexpected url: " + v2);
            }

            v1_2 = v1_2.url(v3).addHeader("Accept-Charset", this.d);
            String v2_1 = this.c.toString();
            v1_2 = v2_1.isEmpty() ? v1_2.removeHeader("Cache-Control") : v1_2.header("Cache-Control", v2_1);
            ResponseBody v1_3 = this.client.newCall(v1_2.build()).execute().body;
            if (v1_3 == null) {
                return v0;
            }

            v0 = v1_3.byteStream();
        } catch (IOException v1_1) {
            Log.e("BaseSuggestionsModel", "Problem getting search suggestions", ((Throwable) v1_1));
        }

        return v0;
    }
}

